package smartbus;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.BuslistBean;
import com.example.myapplication.apter.BuslistApter;

public class SmartbusActivity extends AppCompatActivity {

    private ImageView but_lawyerList;
    private RecyclerView re_smartbus;
    private TextView newRoute;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smartbus);
        initview();
        //返回
        but_bloak();
        //巴士列表
        buslist();
        //点击新建路线
        clicknewroute();
    }

    private void clicknewroute() {
        newRoute.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SmartbusActivity.this,NewrouteActivity.class );
                SmartbusActivity.this.startActivity(intent);
            }
        });
    }

    private void buslist() {
        OKHttpTool.doNetRequest("prod-api/api/bus/line/list",
                OKHttpTool.GET,
                BuslistBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                BuslistBean buslistBean = (BuslistBean)obj;
                                if(buslistBean.getCode()==200){
                                    BuslistApter buslistApter = new BuslistApter(SmartbusActivity.this,buslistBean.getRows());
                                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(SmartbusActivity.this);
                                    re_smartbus.setLayoutManager(linearLayoutManager);
                                    re_smartbus.setAdapter(buslistApter);
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void but_bloak() {
        but_lawyerList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SmartbusActivity.this.finish();
            }
        });
    }

    private void initview() {
        but_lawyerList = findViewById(R.id.but_lawyerList);
        re_smartbus = findViewById(R.id.re_smartbus);
        newRoute = findViewById(R.id.NewRoute);
    }
}